package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AbShehuijiuzhuMapper;
import com.sinog.biz.mapper.havexml.UploadAppendixFileMapper;
import com.sinog.biz.service.business.BjgzShehuijiuzhuInfoMainService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.AbShehuijiuzhuResultVo;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 帮教志愿者
 * @author sunsf
 * @date 2020-02-07-16:40
 */
@Service
public class BjgzShehuijiuzhuInfoMainServiceImpl extends ServiceImpl<AbShehuijiuzhuMapper,AbShehuijiuzhuResultVo> implements BjgzShehuijiuzhuInfoMainService {

    @Resource
    private AbShehuijiuzhuMapper aMapper;
    @Resource
    private UploadAppendixFileMapper uploadAppendixFileMapper;

    /**
     * 保存帮教--志愿者
     * @param dataStr dataStr
     * @return Result
     */
    @Override
    public Result saveBjShzzInfo(String dataStr,UserDTO user) {
        Date date = new Date();
        AbShehuijiuzhuResultVo arv = JsonUtil.toBean(dataStr,AbShehuijiuzhuResultVo.class);
        //新增
        if(StringUtils.isBlank(arv.getId())) {
            arv.setId(IdGen.strid());
            arv.setCreateTime(date);
            arv.setCreateUser(user.getUsername());
        }
        arv.setUpdateTime(date);
        arv.setUpdateUser(user.getUsername());
        boolean tag = arv.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取帮教--社会组织--信息
     * @param pp pp
     * @return Result
     */
    @Override
    public Result getBjShzzBaseInfo(String pp) {
        Map<String,Object> map = aMapper.getBjShzzBaseInfoNew(pp);
        List<Map<String,Object>> appendixBjShzzList = uploadAppendixFileMapper.getUploadAppendixFile(pp,"17",null);
        if(MapUtils.isNotEmpty(map)) {
            map.put("appendixBjShzzList",appendixBjShzzList);
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }
}